﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "Building.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"

// 平台头文件
#include "WeakPtr.h"
#include "IElementParentReporter.h"
#include "IElementBasicInformation.h"
#include "ElementId.h"
#include "IGenericElement.h"
#include "DbObjectUtils.h"
#include "BuildingUtils.h"
#include "IElementCopyPasteComponent.h"
#include "IElementDeletionComponent.h"
#include "IElementStatus.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

#pragma region 基本属性
DBOBJECT_DATA_DEFINE(Building)
{
    m_pOwnerElement = nullptr;
}

IDocument* gcmp::Building::GetDocument() const
{
    if (!m_pOwnerElement)
    {
        return nullptr;
    }
    return m_pOwnerElement->GetDocument();
}

gcmp::NdbObject* gcmp::Building::GetTopOwnerObject() const
{
    return quick_cast<NdbObject>(const_cast<IElement*>(GetOwnerElement()));
}

#pragma endregion 基本属性

#pragma region 关联更新

void gcmp::Building::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    //分区信息
    IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElement, L"IElement为空",L"GDMPLab",L"2024-03-30");
}

void gcmp::Building::ReportParents(IElementParentReporter& reporter) const
{

}

#pragma endregion 关联更新

#pragma region 其他
bool gcmp::Building::FinishEditing()
{
    return true;
}
#pragma endregion 其他

#pragma region protected

bool Building::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

#pragma endregion protected

Building *gcmp::Building::Create(IDocument *pDoc, const std::wstring &name)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(BuildingUtils::GetBuildingByName(pDoc, name) == nullptr, L"单体已经存在了",L"GDMPLab",L"2024-03-30");

    OwnerPtr<Building> opBuilding = NEW_AS_OWNER_PTR(Building);

    IGenericElement* pElement = IGenericElement::Create(pDoc, TransferOwnership(opBuilding), Building::GetClassId().GetGuid(), ElementCreationOptions::Normal);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"创建单体失败",L"GDMPLab",L"2024-03-30");

    IElementBasicInformation* pElemBasicInfo = pElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElemBasicInfo, L"Element basic information is null.",L"GDMPLab",L"2024-03-30");
    pElemBasicInfo->SetClassificationUid(Building::GetClassId().GetGuid());
    pElemBasicInfo->SetCategoryUid(BuiltInCategoryUniIdentities::BICU_BUILDING);
    pElemBasicInfo->SetName(name);

    pElement->GetStatus()->SetIsVisible(false);
    return quick_cast<Building>(pElement->GetExternalObject());
}

std::wstring gcmp::Building::GetName() const
{
    const IElement *pOwnerElement = this->GetOwnerElement();
    DBG_WARN_AND_RETURN_UNLESS(pOwnerElement, L"", L"pOwnerElement为空",L"GDMPLab",L"2024-03-30");
    const IElementBasicInformation* pBasicInfo = pOwnerElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_UNLESS(pBasicInfo, L"", L"Element没有IElementBasicInformation",L"GDMPLab",L"2024-03-30");
    return pBasicInfo->GetName();
}

